import pandas as pd
import yaml
from yaml import SafeLoader

from datareport.api.DataSource import DataSource
from datareport.impl.paragraph.base.BaseParagraph import BaseParagraph
from datareport.api.annotation.Style import style
from datareport.api.enum.ParagraphStyleEnum import ParagraphStyleEnum


@style()
class JiCeng_GuanMoQingKuang_30(BaseParagraph):
    """
    基层党支部
    """

    def __init__(self):
        super().__init__()
        self.text = '''3、从基层党支部来看: 本年度同时被观摩和参加观摩的党支部有{}个，占被观摩党支部的{}。\
其中，{}等{}个党支部被观摩两次'''
        self.sort = 20
        self.section = 6

    def plot(self, df, plt):
        pass

    def getData(self, result):
        pass

    def start(self, conn, year, plt):
        # 被观摩
        obed_df=pd.read_sql('''
        select *  from t_meetingplan where isObserve=1 and year(startTime)={}
        '''.format(year),con=conn)
        # 参加观摩
        part_df=pd.read_sql('''
        select ob.*,(select partyBranchName from t_observer 
                    where t_observer.observer like concat('%',ob.code,'%')) partyBranchName 
                    from t_observerrecord ob where year(createTime)={}
        '''.format(year),con=conn)
        obed_count=len(obed_df)
        obed_part_count=len(obed_df[obed_df['partyBranchName'].isin(part_df['partyBranchName'].drop_duplicates())])
        # 被观摩2次
        sum_df=obed_df.groupby('partyBranchName')['isObserve'].sum()
        two_count_arr=sum_df[sum_df==2].index.values
        self.text=self.text.format(obed_part_count,round(100*obed_part_count/obed_count,1),'、'.join(two_count_arr),len(two_count_arr))
        
if __name__ == '__main__':
    with open('D:\work\sanhuiyike\datareport\config.yaml', encoding='utf-8') as f:
        data = yaml.load(f, Loader=SafeLoader)
    con = DataSource(data['datasource']).conn
    JiCeng_GuanMoQingKuang().start(con, 2023, '')

